import { getDepartmentsListAPI, deleteDepartmentsAPI, getSimpleEmployeeAPI, addDepartmentsAPI, findDepartmentsAPI, updateDepartmentsAPI } from '@/api/departmentsApi'
import { transitionToTreeData, transToTreeData } from '@/utils'

const actions = {
  /* 发送请求获取部门列表的action */
  async fetchDepartmentsList(context) {
    const { data: { depts: departmentsList }, message } = await getDepartmentsListAPI()
    context.commit('setDepartmentsList', departmentsList.splice(1))
    return message
  },
  /* 发送请求删除部门的action */
  async deleteDepartments(context, id) {
    const { message } = await deleteDepartmentsAPI(id)
    return message
  },
  /* 发送请求添加部门的action */
  async addDepartments(context, data) {
    const { message } = await addDepartmentsAPI(data)
    return message
  },
  /* 发送请求查询部门的action */
  async findDepartments(context, id) {
    const { data } = await findDepartmentsAPI(id)
    return data
  },
  /* 发送请求修改部门的action */
  async updateDepartments(context, data) {
    const { message } = await updateDepartmentsAPI(data)
    return message
  },
  /* 发送请求获取部门负责人的action */
  async fetchSimpleEmployees(context) {
    const { data, message } = await getSimpleEmployeeAPI()
    context.commit('setSimpleEmployees', data)
    return message
  }

}
const mutations = {
  /* 设置企业部门列表的mutation */
  setDepartmentsList(state, departmentsList) {
    state.departmentsList = departmentsList
  },
  setSimpleEmployees(state, persons) {
    state.persons = persons
  }
}
const state = () => {
  return {
    /* 企业部门列表 */
    departmentsList: [],
    /* 部门负责人 */
    persons: [],
    company: { name: '江苏传智博客教育科技有限公司', manager: '负责人', id: '' }
  }
}
const getters = {
  treeDepartmentsList: state => transToTreeData(state.departmentsList, '') || []
}

export default {
  actions,
  mutations,
  state,
  getters,
  namespaced: true
}
